package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.bean.User;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.RedisKeys;
import com.itheima.reggie.common.SendSms;
import com.itheima.reggie.common.ValidateCodeUtils;
import com.itheima.reggie.controllerApi.UserControllerApi;
import com.itheima.reggie.dto.LoginDto;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 * User: 许志民
 * Date: 2021-11-27
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController implements UserControllerApi {
    @Autowired
    private UserService userService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody User user){
        //发送验证码
        Integer integer = ValidateCodeUtils.generateValidateCode(6);
        System.out.println("发送的验证码为"+integer);
//        SendSms.sendMsg(user.getPhone(),integer+"");
        HttpSession session = request.getSession();
        session.setAttribute(user.getPhone(),integer);
        redisTemplate.opsForValue().setIfAbsent(RedisKeys.RG_CODE+user.getPhone(),integer,5, TimeUnit.MINUTES);
        return R.success("发送成功");
    }

    @PostMapping("/login")
    public R loginUser(@RequestBody LoginDto loginDto){
        if(loginDto == null){
            return R.error("参数异常");
        }
        HttpSession session = request.getSession();
//        Integer code = (Integer) session.getAttribute(loginDto.getPhone());
        Integer redisCode = (Integer) redisTemplate.opsForValue().get(RedisKeys.RG_CODE + loginDto.getPhone());

        System.out.println("接收到验证码为"+redisCode);
        if(redisCode == null){
            return R.error("请先获取验证码");
        }
        if(redisCode != Integer.parseInt(loginDto.getCode())){
            return R.error("验证码输入有误");
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone,loginDto.getPhone());
        User user = userService.getOne(wrapper);
        if(user != null){
            redisTemplate.delete(RedisKeys.RG_CODE + loginDto.getPhone());
            session.setAttribute("user",user.getId());
            return R.success("登录成功");
        }
        User newUser = new User();
        newUser.setPhone(loginDto.getPhone());
        userService.save(newUser);
        session.setAttribute("user",newUser.getId());
        //登录成功后移除Redis中的验证码  set(RedisKeys.RG_CODE + loginDto.getPhone(),null);
        Boolean delete = redisTemplate.delete(RedisKeys.RG_CODE + loginDto.getPhone());
        System.err.println(delete);
        return R.success("登录成功");
    }

    @PostMapping("/loginout")
    public R loginOut(){
        HttpSession session = request.getSession();
        session.setAttribute("user",null);
        return R.success("success");
    }
}
